package com.shenjinzheng.dk.auth.controller;

import com.alibaba.nacos.api.model.v2.Result;
import com.shenjinzheng.dk.auth.dto.LoginDTO;
import com.shenjinzheng.dk.auth.dto.SendSmsDTO;
import com.shenjinzheng.dk.auth.enums.AuthType;
import com.shenjinzheng.dk.auth.service.AuthService;
import com.shenjinzheng.dk.auth.service.UserAuthService;
import com.shenjinzheng.dk.auth.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 认证表(存储用户的登录凭证) 前端控制器
 * </p>
 *
 * @author shenjinzheng
 * @since 2025-01-22
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    //存放登录类型(type)的Map集合
    private final Map<String, UserAuthService> userAuthServiceHashMap;

    public AuthController(Map<String, UserAuthService> userAuthServiceHashMap) {
        this.userAuthServiceHashMap = userAuthServiceHashMap;
    }

    @Operation(summary = "用户登录")
    @PostMapping("login")
    public Result<UserLoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("loginDTO:{}", loginDTO);

        UserAuthService userService = userAuthServiceHashMap.get(loginDTO.getType());
        UserLoginVO userLoginVO = userService.login(loginDTO);
        return Result.success(userLoginVO);
    }


    @Operation(summary = "获取验证码")
    @PostMapping("getCode")
    public Result<Object> getCode(@RequestBody SendSmsDTO sendSmsDTO) {

        UserAuthService userAuthService = userAuthServiceHashMap.get(AuthType.VERIFICATION_CODE.getClassName());
        userAuthService.sendCode(sendSmsDTO.getPhone());
        //todo 记录场景使用次数
        return Result.success();
    }

}
